package com.example.skudemo.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisOrderNo {
    private static  final long BEGIN_TIMESTAMP = 1688860800L;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public long nextId(String keyPrefix) {
        // 1.当前时间鹾差
        LocalDateTime now = LocalDateTime.now();
        long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
        long timeStamp = nowSecond - BEGIN_TIMESTAMP;
        // 2.redis序列号(每天内自增)
        String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        Long count = this.redisTemplate.opsForValue().increment("incr:" + keyPrefix + ":" + date);
        return timeStamp << 32 | count;
    }
    public static void main(String[] args) {
        // 获取当前的时间戳
        LocalDateTime time = LocalDateTime.of(2023, 7, 9, 0, 0, 0);
        long l = time.toEpochSecond(ZoneOffset.UTC);
        System.out.println(l);

    }
}
